﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Fluent="clr-namespace:Fluent"
                    xmlns:Behaviours="clr-namespace:Fluent.Metro.Behaviours"
                    xmlns:internal="clr-namespace:Fluent.Internal"
                    xmlns:Converters="clr-namespace:Fluent.Converters">

    <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    <Fluent:IconConverter x:Key="IconConvert" />

    <ControlTemplate x:Key="RibbonWindowControlTemplate"
                     TargetType="{x:Type Fluent:RibbonWindow}">
        <Grid Background="{TemplateBinding Background}">
            <Grid x:Name="root">
                <Border BorderBrush="{DynamicResource RibbonThemeColorBrush}"
                        BorderThickness="1"
                        x:Name="themecolorborder" />
                <AdornerDecorator Margin="1,0,1,0"
                                  x:Name="content">
                    <Grid>
                        <Grid x:Name="PART_TitleBar"
                              Height="{Binding Path=(Fluent:RibbonProperties.TitleBarHeight), RelativeSource={RelativeSource Self}}"
                              VerticalAlignment="Top"
                              Background="Transparent">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Image x:Name="PART_Icon"
                                   Height="16"
                                   Width="16"
                                   Margin="8,0,0,0"
                                   Visibility="{TemplateBinding IsIconVisible, Converter={StaticResource BooleanToVisibilityConverter}}"
                                   Source="{TemplateBinding Icon, Converter={StaticResource IconConvert}}"
                                   Stretch="UniformToFill" />
                        </Grid>
                        <ContentPresenter Content="{Binding WindowCommands, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:RibbonWindow}}}"
                                          x:Name="PART_WindowCommands"
                                          Panel.ZIndex="1"
                                          Grid.RowSpan="2"
                                          VerticalAlignment="Top"
                                          HorizontalAlignment="Right"
                                          Height="{Binding Path=(Fluent:RibbonProperties.TitleBarHeight), RelativeSource={RelativeSource Self}}" />
                        <ContentPresenter />
                    </Grid>
                </AdornerDecorator>
                <ResizeGrip x:Name="WindowResizeGrip"
                            HorizontalAlignment="Right"
                            IsTabStop="false"
                            Visibility="Collapsed"
                            VerticalAlignment="Bottom" />
            </Grid>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsActive"
                     Value="False">
                <Setter Property="Opacity"
                        TargetName="PART_WindowCommands"
                        Value="0.5" />
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ResizeMode"
                               Value="CanResizeWithGrip" />
                    <Condition Property="WindowState"
                               Value="Normal" />
                </MultiTrigger.Conditions>
                <Setter Property="Margin"
                        TargetName="content"
                        Value="1" />
                <Setter Property="Visibility"
                        TargetName="themecolorborder"
                        Value="Visible" />
                <Setter Property="Visibility"
                        TargetName="WindowResizeGrip"
                        Value="Visible" />
            </MultiTrigger>
            <Trigger Property="WindowState"
                     Value="Maximized">
                <Setter Property="Margin"
                        TargetName="root"
                        Value="0,-2,0,0" />                
                <Setter Property="Margin"
                        TargetName="content"
                        Value="0" />
                <Setter Property="Visibility"
                        TargetName="themecolorborder"
                        Value="Collapsed" />
                <Setter Property="Visibility"
                        TargetName="WindowResizeGrip"
                        Value="Collapsed" />
            </Trigger>
        </ControlTemplate.Triggers>

    </ControlTemplate>
      
    <Style TargetType="{x:Type Fluent:WindowCommands}">
        <Setter Property="Foreground"
                Value="White" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Fluent:WindowCommands">
                    <ControlTemplate.Resources>
                        <ResourceDictionary>
                            <Style TargetType="{x:Type Button}">
                                <Setter Property="Background" Value="#02FFFFFF" />
                                <Setter Property="HorizontalContentAlignment" Value="Center" />
                                <Setter Property="VerticalContentAlignment" Value="Center" />
                                <Setter Property="Padding" Value="1" />
                                <Setter Property="Template"
                                        Value="{DynamicResource WindowCommandsControlTemplate}"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ShowTitleBar, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:RibbonWindow}}}" Value="True">
                                        <Setter Property="Foreground" Value="White" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ResourceDictionary>
                    </ControlTemplate.Resources>

                    <StackPanel Orientation="Horizontal"
                                VerticalAlignment="Top">
                        <ItemsControl ItemsSource="{Binding Items, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:WindowCommands}}}"                                      
                                      ItemTemplate="{TemplateBinding ItemTemplate}"
                                      ItemTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                      ItemContainerStyle="{TemplateBinding ItemContainerStyle}"
                                      ItemContainerStyleSelector="{TemplateBinding ItemContainerStyleSelector}">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <StackPanel Orientation="Horizontal" />
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>
                        </ItemsControl>
                        <Button x:Name="PART_Min"
                                Width="29"
                                Height="{Binding Path=(Fluent:RibbonProperties.TitleBarHeight), RelativeSource={RelativeSource Self}}"
                                MaxHeight="19"
                                Margin="0,3,0,0"
                                Style="{DynamicResource IronicallyNamedChromelessButtonStyle}"
                                Padding="0"
                                Foreground="{TemplateBinding Foreground}"
                                ToolTip="{Binding Minimize, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:WindowCommands}}}"
                                Uid="{Binding Minimize, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:WindowCommands}}}">
                            <Path Data="F1M0,6L0,9 9,9 9,6 0,6z"
                                  SnapsToDevicePixels="True"
                                  Fill="{DynamicResource TabItemFontBrush}" />
                        </Button>

                        <Button x:Name="PART_Max"
                                Width="29"
                                Height="{Binding Path=(Fluent:RibbonProperties.TitleBarHeight), RelativeSource={RelativeSource Self}}"
                                MaxHeight="19"
                                Margin="0,3,0,0"
                                Style="{DynamicResource IronicallyNamedChromelessButtonStyle}"
                                Padding="0"
                                Foreground="{TemplateBinding Foreground}"
                                ToolTip="{Binding Maximize, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:WindowCommands}}}"
                                Uid="{Binding Maximize, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:WindowCommands}}}">
                            <Grid>
                                <Path x:Name="PART_MaximizeButtonContent"
                                      SnapsToDevicePixels="True"
                                      Data="F1M0,0L0,9 9,9 9,0 0,0 0,3 8,3 8,8 1,8 1,3z"
                                      Fill="{DynamicResource TabItemFontBrush}" />
                                <Path x:Name="PART_RestoreButtonContent"
                                      SnapsToDevicePixels="True"
                                      Data="F1M0,10L0,3 3,3 3,0 10,0 10,2 4,2 4,3 7,3 7,6 6,6 6,5 1,5 1,10z M1,10L7,10 7,7 10,7 10,2 9,2 9,6 6,6 6,9 1,9z"
                                      Fill="{DynamicResource TabItemFontBrush}" />
                            </Grid>
                        </Button>

                        <Button x:Name="PART_Close"
                                Width="29"
                                Height="{Binding Path=(Fluent:RibbonProperties.TitleBarHeight), RelativeSource={RelativeSource Self}}"
                                MaxHeight="19"
                                Margin="0,3,0,0"
                                Style="{DynamicResource IronicallyNamedChromelessButtonStyle}"
                                Padding="0"
                                Foreground="{TemplateBinding Foreground}"
                                ToolTip="{Binding Close, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:WindowCommands}}}"
                                Uid="{Binding Close, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Fluent:WindowCommands}}}">
                            <Path Data="F1M0,0L2,0 5,3 8,0 10,0 6,4 10,8 8,8 5,5 2,8 0,8 4,4 0,0z"
                                  SnapsToDevicePixels="True"
                                  Fill="{DynamicResource TabItemFontBrush}" />
                        </Button>
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding ResizeMode, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                                     Value="NoResize">
                            <Setter Property="Visibility"
                                    TargetName="PART_Min"
                                    Value="Collapsed" />                            
                            <Setter Property="Visibility"
                                    TargetName="PART_Max"
                                    Value="Collapsed" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ResizeMode, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
                                     Value="CanMinimize">
                            <Setter Property="Visibility"
                                    TargetName="PART_Max"
                                    Value="Collapsed" />
                        </DataTrigger>                        
                    </ControlTemplate.Triggers>
                </ControlTemplate>                
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate TargetType="{x:Type Button}"
                     x:Key="WindowCommandsControlTemplate">
        <Grid Background="{TemplateBinding Background}">
            <ContentPresenter x:Name="contentPresenter"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                              RecognizesAccessKey="True"
                              Opacity="0.75"
                              Margin="11,1,11,1" />

            <Rectangle x:Name="border"
                       SnapsToDevicePixels="True"
                       IsHitTestVisible="False"
                       Opacity="0.25"
                       Width="0"
                       Height="15"
                       VerticalAlignment="Stretch"
                       HorizontalAlignment="Right"
                       Fill="{TemplateBinding Foreground}"
                       Margin="10,0,0,0" />
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver"
                     Value="True">
                <Setter TargetName="contentPresenter"
                        Property="Opacity"
                        Value="1" />
            </Trigger>
            <Trigger Property="IsMouseOver"
                     Value="False">
                <Setter TargetName="contentPresenter"
                        Property="Opacity"
                        Value=".5" />
            </Trigger>
            <Trigger Property="IsKeyboardFocused"
                     Value="true" />
            <Trigger Property="ToggleButton.IsChecked"
                     Value="true" />
            <Trigger Property="IsEnabled"
                     Value="false">
                <Setter Property="Foreground"
                        Value="#ADADAD" />
            </Trigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource PreviousData}}"
                         Value="{x:Null}">
                <Setter TargetName="border"
                        Property="Rectangle.Width"
                        Value="1" />
            </DataTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <Style x:Key="RibbonWindowStyle"
           TargetType="{x:Type Fluent:RibbonWindow}">
        <Setter Property="ResizeBorderThickness"
                Value="8" />
        <Setter Property="BorderThickness"
                Value="0" />
        <Setter Property="CornerRadius"
                Value="0" />
        <!-- Cheat with GlassBorderThickness. We have to set something else than 0 to avoid some bugs in the WindowChrome class. -->
        <Setter Property="GlassBorderThickness"
                Value="0,0,0,1" />
        <Setter Property="DontUseDwm"
                Value="True" />
        <Setter Property="Foreground">
            <Setter.Value>
                <SolidColorBrush Color="{DynamicResource BlackColor}" />
            </Setter.Value>
        </Setter>
        <Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush Color="{DynamicResource WhiteColor}" />
            </Setter.Value>
        </Setter>
        <Setter Property="Behaviours:StylizedBehaviors.Behaviors">
            <Setter.Value>
                <Behaviours:StylizedBehaviorCollection>
                    <Behaviours:BorderlessWindowBehavior />
                    <Behaviours:WindowsSettingBehavior />
                </Behaviours:StylizedBehaviorCollection>
            </Setter.Value>
        </Setter>
        <Setter Property="Template"
                Value="{StaticResource RibbonWindowControlTemplate}" />
        <Style.Triggers>
            <Trigger Property="WindowState"
                     Value="Maximized">
                <Setter Property="ResizeBorderThickness"
                        Value="0" />
            </Trigger>            
        </Style.Triggers>
    </Style>
</ResourceDictionary>